package com.cloud.code.lambda;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.Comparator;
import java.util.TreeSet;

@Slf4j
public class examples {

    @Test
    public void test1(){
        /**
         * 简化之前
         */
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("我是一个线程...");
            }
        }).start();

        /**
         * 简化之后
         */
        new Thread(() -> {
            System.out.println("我是简化后的写法...");
        }).start();
    }


    @Test
    public void test2(){

        TreeSet<Employee> employees = new TreeSet<>(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                return Double.compare(o1.getSalary(),o2.getSalary());
            }
        });
        employees.add(new Employee(1,"小李",10000.00));
        employees.add(new Employee(2,"小张",20000.00));

        /**
         * 简化后
         */
//        TreeSet<Employee> employee2 = new TreeSet<>(Comparator.comparingDouble(Employee::getSalary));
        TreeSet<Employee> employee2 = new TreeSet<>((o1, o2)->{
            return Double.compare(o2.getSalary(),o1.getSalary());
        });
        employee2.add(new Employee(1,"小李",10000.00));
        employee2.add(new Employee(2,"小张",20000.00));

        log.debug(JSON.toJSONString(employee2));
    }




}
